import asyncio
from flask import Flask, jsonify, request, send_file
import textToAudio
# 创建Flask应用
app = Flask(__name__)
voice = 'zh-CN-YunxiNeural'
rate = '+20%'
volume = '+0%'

# 设置路由
@app.route('/convert', methods=['POST'])
def my_api():
    data = request.json
    text = data["text"]
    voice = data["voice"]
    rate = data["rate"]
    volume = data["volume"]
    fileText = asyncio.run(textToAudio.textAudio(text=text,voice=voice,rate=rate,volume=volume))
    return jsonify({"code": '000000', "msg": "语音生成成功", "fileId": fileText})
# 设置路由
@app.route('/downFile', methods=['POST'])
def my_api1():
    data = request.json
    fileId = data["fileId"]
    file_path = 'files/' + fileId  # 文件路径
    return send_file(file_path, mimetype='audio/mpeg',as_attachment=False,)

if __name__ == '__main__':
    app.run(debug=True)
